﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.DepartmentManage
{
    public partial class Modify : Common.PageBase
    {

        T_USER_BLL userBll = new T_USER_BLL();
        T_DEPARTMENT_BLL bll = new T_DEPARTMENT_BLL();
        T_BUSINESSDIVISION_BLL bdBll = new T_BUSINESSDIVISION_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
            //PageHelper.SetPopCalender(this.Page, txtCREATETIME);
            //PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIME);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlBUSINESSDIVISIONIDBinding();
                ddlPDEPARTMENTIDBinding();
                ddlSTATUSBinding();
                ddlISCENTERBinding();

                pageDataInit();
            }
        }

        #region 页面初始化数据绑定

        /// <summary>
        ///	上级部门编号 下拉列表数据绑定
        /// </summary>
        private void ddlPDEPARTMENTIDBinding()
        {
            string businessDivisionCode = string.Empty;
            T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
            if (bdEntity != null)
            {
                businessDivisionCode = bdEntity.BUSINESSDIVISIONCODE;
            }


            T_DEPARTMENT_BLL bll = new T_DEPARTMENT_BLL();
            bll.FillListControl(ddlPDEPARTMENTID, "0", businessDivisionCode, "");



            //ddlPDEPARTMENTID.DataSource = null;
            //ddlPDEPARTMENTID.DataTextField = "";
            //ddlPDEPARTMENTID.DataValueField = "";
            //ddlPDEPARTMENTID.DataBind();
            ddlPDEPARTMENTID.Items.Insert(0, new ListItem("", "0"));
        }


        private void ddlBUSINESSDIVISIONIDBinding()
        {
            ddlBUSINESSDIVISIONID.DataSource = bdBll.GetCacheList(this.Page, String.Format(" and Status = {0} ", ((int)Status.启用).ToString()), "Status");
            ddlBUSINESSDIVISIONID.DataTextField = "BUSINESSDIVISIONNAME";
            ddlBUSINESSDIVISIONID.DataValueField = "BUSINESSDIVISIONID";
            ddlBUSINESSDIVISIONID.DataBind();
            if (!UserLoginInfo.IsAdministrator())
            {
                ddlBUSINESSDIVISIONID.SelectedValue = _userLoginInfo.UserEntity.BUSINESSDIVISIONID.ToString();
                ddlBUSINESSDIVISIONID.Enabled = false;
            }

        }


        private void ddlSTATUSBinding()
        {
            ddlSTATUS.Items.Clear();
            EnumHelper.FillListControl(ddlSTATUS, typeof(Status));
        }

        private void ddlISCENTERBinding()
        {
            ddlISCENTER.Items.Clear();
            EnumHelper.FillListControl(ddlISCENTER, typeof(DepartmentType));
        }



        




        #endregion

        private void pageDataInit()
        {
            string _departmentid = String.IsNullOrEmpty(Request.QueryString["DEPARTMENTID"]) ? "" : Request.QueryString["DEPARTMENTID"].Trim();

            if (!String.IsNullOrEmpty(_departmentid))
            {
                T_DEPARTMENT_Entity entity = bll.GetEntity("DEPARTMENTID", _departmentid);

                PageHelper.SetControlValue<T_DEPARTMENT_Entity>(this.Page, entity);
                ddlBUSINESSDIVISIONID_SelectedIndexChanged(null, null);
                ddlPDEPARTMENTID.SelectedValue = entity.PDEPARTMENTID.ToString();
                ddlPDEPARTMENTID.Enabled = false;
                ddlBUSINESSDIVISIONID.Enabled = false;
                lblTitle.Text = "部门修改";
            }
            else
            {
                lblTitle.Text = "部门添加";

            }

            txtCREATEPEOPLE.Enabled = false;
            txtCREATEPEOPLEID.Enabled = false;
            txtCREATETIME.Enabled = false;
            txtLASTMODIFYPEOPLE.Enabled = false;
            txtLASTMODIFYPEOPLEID.Enabled = false;
            txtLASTMODIFYTIME.Enabled = false;
            if (!UserLoginInfo.IsAdministrator())
            {
                ddlBUSINESSDIVISIONID.SelectedValue = _userLoginInfo.UserEntity.BUSINESSDIVISIONID.ToString();
                ddlBUSINESSDIVISIONID.Enabled = false;
            }

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _departmentid = String.IsNullOrEmpty(Request.QueryString["DEPARTMENTID"]) ? "" : Request.QueryString["DEPARTMENTID"].Trim();

            string oldDepartmentCode = "";
            string oldDepartmentName = "";


            T_DEPARTMENT_Entity entity = bll.GetEntity("DEPARTMENTID", _departmentid);

            if (entity != null)
            {
                oldDepartmentCode = entity.DEPARTMENTCODE;
                oldDepartmentName = entity.DEPARTMENTNAME;

                PageHelper.GetControlValue<T_DEPARTMENT_Entity>(this.Page, ref entity);
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;
                T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
                if (bdEntity != null)
                {
                    entity.BUSINESSDIVISIONCODE = bdEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONNAME = bdEntity.BUSINESSDIVISIONNAME;
                }
                

                if (ddlPDEPARTMENTID.SelectedValue == "0")
                {
                    entity.DEPARTMENTDEPTH = 1;
                    entity.PDEPARTMENTFULLID = "";
                }
                else
                {
                    T_DEPARTMENT_Entity parentEntity = bll.GetEntity("DEPARTMENTID", ddlPDEPARTMENTID.SelectedValue.Trim());
                    if (parentEntity != null)
                    {
                        entity.DEPARTMENTDEPTH = parentEntity.DEPARTMENTDEPTH.Value + 1;
                        if (String.IsNullOrEmpty(parentEntity.PDEPARTMENTFULLID))
                        {
                            entity.PDEPARTMENTFULLID = parentEntity.DEPARTMENTID.ToString();
                        }
                        else
                        {
                            entity.PDEPARTMENTFULLID = parentEntity.PDEPARTMENTFULLID + "-" + parentEntity.DEPARTMENTID.ToString();
                        }
                    }
                    else
                    {
                        entity.DEPARTMENTDEPTH = 1;
                        entity.PDEPARTMENTFULLID = "";
                    }
                }

                try
                {
                    long rel = bll.Update(entity);
                    if (rel > 0)
                    {
                        if (oldDepartmentCode != entity.DEPARTMENTCODE || oldDepartmentName != entity.DEPARTMENTNAME)
                        {
                            userBll.UpdateDepartment(entity.DEPARTMENTID.ToString(), entity.DEPARTMENTCODE.Trim(), entity.DEPARTMENTNAME, _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE, UserType.中心部门);
                        }
                        PageHelper.ShowMessage(this.Page, "修改成功！", UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this), "list.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "修改失败！");
                    }
                }
                catch (Exception ex)
                {
                        PageHelper.ShowMessage(this.Page, "修改失败！" + ex.Message);
                }
            }
            else
            {
                entity = new T_DEPARTMENT_Entity();

                PageHelper.GetControlValue<T_DEPARTMENT_Entity>(this.Page, ref entity);
                entity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.CREATETIME = DateTime.Now;
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;
                T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
                if (bdEntity != null)
                {
                    entity.BUSINESSDIVISIONCODE = bdEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONNAME = bdEntity.BUSINESSDIVISIONNAME;
                }

                if (ddlPDEPARTMENTID.SelectedValue == "0")
                {
                    entity.DEPARTMENTDEPTH = 1;
                    entity.PDEPARTMENTFULLID = "";
                }
                else
                {
                    T_DEPARTMENT_Entity parentEntity = bll.GetEntity("DEPARTMENTID", ddlPDEPARTMENTID.SelectedValue.Trim());
                    if (parentEntity != null)
                    {
                        entity.DEPARTMENTDEPTH = parentEntity.DEPARTMENTDEPTH.Value + 1;
                        if (String.IsNullOrEmpty(parentEntity.PDEPARTMENTFULLID))
                        {
                            entity.PDEPARTMENTFULLID = parentEntity.DEPARTMENTID.ToString();
                        }
                        else
                        {
                            entity.PDEPARTMENTFULLID = parentEntity.PDEPARTMENTFULLID + "-" + parentEntity.DEPARTMENTID.ToString();
                        }
                    }
                    else
                    {
                        entity.DEPARTMENTDEPTH = 1;
                        entity.PDEPARTMENTFULLID = "";
                    }
                }
                try
                {

                    long rel = bll.Insert(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "添加成功！", "list.aspx");
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "添加失改！");
                    }
                }
                catch (Exception ext)
                {
                    
                        PageHelper.ShowMessage(this.Page, "添加失改！" + ext.Message);
                }

            }
        }

        protected void ddlBUSINESSDIVISIONID_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlPDEPARTMENTIDBinding();
            ddlPDEPARTMENTID_SelectedIndexChanged(null, null);
        }

        protected void ddlPDEPARTMENTID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(!String.IsNullOrEmpty(ddlPDEPARTMENTID.SelectedValue.Trim()))
            {
                T_DEPARTMENT_Entity pEntity = bll.GetEntity("DEPARTMENTID", ddlPDEPARTMENTID.SelectedValue.Trim());
                if(pEntity != null)
                {
                    ddlISCENTER.SelectedValue = pEntity.ISCENTER.ToString();
                }
            }
        }
    }
}
